package com.itheima;

import java.util.Scanner;

public class CalculatorTest {
    public static void Calculator1() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入算式（num op num）：");
        String str = sc.nextLine();
        String char1 = str.substring(0, 1);
        int num1 = Integer.parseInt(char1);
        String char2 = str.substring(1,2);
        char op = char2.charAt(0);
        String char3 = str.substring(2,3);
        int num2 = Integer.parseInt(char3);
        calculation(num1, op, num2);
    }

    public static void Calculator2(){
        Scanner sc = new Scanner(System.in);
        System.out.println("第一个数：");
        int num1 = sc.nextInt();
        System.out.println("运算符：");
        char op = sc.next().charAt(0);
        System.out.println("第二个数：");
        int num2 = sc.nextInt();
        calculation(num1, op, num2);
    }

    private static void calculation(int num1, char op, int num2) {
        switch (op){
            case '+':
                System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
                break;
            case '-':
                System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
                break;
            case '*':
                System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
                break;
            case '/':
                if (num2 != 0) {
                    System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
                } else {
                    System.out.println("除数不能为0！");
                }
                break;
            default:
                System.out.println("无效的运算符！");
        }
    }

    public static void main(String[] args) {
        Calculator1();
        Calculator2();
    }
}
